<template>
    <div class="login-container">
        <div class="imgPhoto">
            <img alt src="@/module-dashboard/assets/logo/logo1.png" style="height: 45px " @click="goVue">
            <img alt src="@/module-dashboard/assets/logo/em.svg" style="width: 120px; height: 45px" @click="goEm">
            <img alt src="@/module-dashboard/assets/logo/git.svg" style="width: 120px; height: 45px" @click="goGit">
            <img alt src="@/module-dashboard/assets/logo/ng.png" style="width: 120px; height: 45px" @click="goNg">
            <img alt src="@/module-dashboard/assets/logo/ms.png" style="width: 120px; height: 45px" @click="goMs">
            <el-image src="https://cloudcache.tencent-cloud.com/qcloud/portal/kit/images/slice/logo.23996906.svg"
                style="width: 120px; height: 45px" @click="goTc" />
        </div>
        <div class="loginBox">
            <!--提示  -->
            <div style="display: flex; margin-top: 20px; height: 100px;">
                <transition name="el-fade-in-linear">
                    <div v-show="show" class="transition-box">
                        <!-- 文字 -->
                        <p class="oneWord">乐酷刷题系统</p>
                    </div>
                </transition>
            </div>
            <!-- 登录 -->
            <el-form v-show="dengLu2" ref="loginForm" :model="loginForm" :rules="loginRules" autocomplete="on"
                class="login-form" label-position="left">
                <div class="title-container">
                    <!-- <h3 class="title">{{$t('login.title')}}</h3> -->
                    <div class="logoInfo clearfix">
                        <em class="logo" />
                        <span class="logoFontZ" style="color:#777" width="130px">乐酷刷题系统</span>
                        <span padding-left="20px" style="color:#777" width="130px">用户登录</span>
                    </div>
                    <lang-select class="set-language" />
                </div>
                <el-form-item :class="{ isBorder: borderr }" prop="username"
                    style="background-color: rgba(54, 53, 53, 0.615); ">
                    <span class="svg-container svg-container_login">
                        <svg-icon icon-class="user" />
                    </span>
                    <el-input v-model="loginForm.username" autocomplete="on" name="username" placeholder="username"
                        type="text" @blur="borderr = true" @focus="borderr = false" />
                </el-form-item>

                <el-form-item :class="{ isBorder: borderrr }" prop="password"
                    style="background-color: rgba(54, 53, 53, 0.615);">
                    <span class="svg-container">
                        <svg-icon icon-class="password" />
                    </span>
                    <el-input v-model="loginForm.password" :type="passwordType" autocomplete="on" name="password"
                        placeholder="password" @blur="borderrr = true" @focus="borderrr = false"
                        @keyup.enter.native="handleLogin" />
                    <span class="show-pwd" @click="showPwd">
                        <svg-icon icon-class="eye" />
                    </span>
                </el-form-item>
                <!-- 新增账号密码框为空 按钮禁用功能 -->
                <el-button :disabled="!canSubmit" :loading="loading"
                    style="width:100%;margin-bottom:30px; background-color: rgba(54, 53, 53, 0.315); border: none;"
                    type="primary" @click.native.prevent="handleLogin">{{ $t('login.logIn') }}</el-button>
            </el-form>
        </div>
    </div>
</template>

<script>
import { validateEmail } from '@/utils/validate'
import LangSelect from '@/components/LangSelect'
import shajs from 'sha.js'
export default {
    name: 'Login',
    components: { LangSelect },
    data() {
        const validateUsername = (rule, value, callback) => {
            if (!validateEmail(value)) {
                callback(new Error('Please enter the email user name'))
            } else {
                callback()
            }
        }
        const validatePassword = (rule, value, callback) => {
            if (value.length < 6) {
                callback(new Error('The password can not be less than 6 digits'))
            } else {
                callback()
            }
        }
        return {
            loginForm: {
                username: 'root@admin.com',
                password: '123456'
            },
            loginRules: {
                username: [{ required: true, trigger: 'blur', validator: validateUsername }],
                password: [{ required: true, trigger: 'blur', validator: validatePassword }]
            },
            passwordType: 'password',
            loading: false,
            showDialog: false,
            // 图片
            // fill / contain / cover / none / scale-down
            fits: ['fill', 'contain', 'cover', 'none', 'scale-down'],
            borderr: true,
            borderrr: true,
            dengLu2: false,
            // 欢迎语
            show: false,
            url: ''

        }
    },
    computed: {
        canSubmit() {
            const { username, password } = this.loginForm
            return Boolean(username && password)
        }
    },
    mounted() {
        this.showFn()
    },

    destroyed() { },
    methods: {
        goTc() {
            this.url = 'https://cloud.tencent.com/'
            window.open(this.url)
        },
        goMs() {
            this.url = 'https://www.mysql.com/'
            window.open(this.url)
        },
        goNg() {
            this.url = 'https://nginx.org/en/'
            window.open(this.url)
        },
        goEm() {
            this.url = 'https://element.eleme.cn/#/zh-CN'
            window.open(this.url)
        },
        goVue() {
            this.url = 'https://cn.vuejs.org/'
            window.open(this.url)
        },
        goGit() {
            this.url = 'https://gitee.com/'
            window.open(this.url)
        },
        showFn() {
            setTimeout(() => {
                this.show = true
            }, 2000)
            // dengLu2
            setTimeout(() => {
                this.show = false
                this.dengLu2 = true
            }, 5000)
        },
        showPwd() {
            if (this.passwordType === 'password') {
                this.passwordType = ''
            } else {
                this.passwordType = 'password'
            }
        },
        handleLogin() {
            this.$refs.loginForm.validate(async (valid) => {
                if (valid) {
                    this.loading = true
                    await this.$store
                        .dispatch('LoginByUsername', {
                            username: this.loginForm.username,
                            password: shajs('sha256').update(this.loginForm.password).digest('hex')
                        })
                        .then(() => {
                            this.loading = false
                            this.$router.push({ path: '/' })
                        })
                        .catch(() => {
                            this.loading = false
                        })
                } else {
                    // console.log('error submit!!')
                    return false
                }
            })
        }
    }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg: #2d3a4b;
$light_gray: #333;

/* reset element-ui css */
.login-container {
    .el-input {
        display: inline-block;
        height: 47px;
        width: 85%;

        input {
            background: transparent;
            border: 0px;
            -webkit-appearance: none;
            border-radius: 0px;
            padding: 12px 5px 12px 15px;
            color: $light_gray;
            height: 47px;
            color: #fff;
            font-weight: 800;

            &:-webkit-autofill {
                -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
                -webkit-text-fill-color: #fff !important;
            }
        }
    }

    .el-form-item {
        border: 1px solid #dcdfe6;
        background: #fff;
        border-radius: 5px;
        color: #454545;
    }

    .el-button--medium {
        height: 50px;
        line-height: 20px;
        font-size: 22px;
    }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #333;

.login-container {
    position: fixed;
    height: 100%;
    width: 100%;
    background-color: $bg;

    background-image: url('./../assets/bg.gif');
    // -moz-background-size: 100% 100%;
    // background-size: 100% 100%;
    background-repeat: repeat;

    .loginBox {
        height: 100%;
        width: 100%;
    }

    .login-form {
        position: absolute;
        left: 30%;
        top: 50%;
        width: 520px;
        padding: 35px 35px 15px 35px;
        margin: -200px 0 0 0;
        // background: #f5f5f5;
    }

    .tips {
        font-size: 14px;
        // color: #fff;
        margin-bottom: 10px;

        span {
            &:first-of-type {
                margin-right: 16px;
            }
        }
    }

    .svg-container {
        padding: 6px 5px 6px 15px;
        color: $dark_gray;
        vertical-align: middle;
        width: 30px;
        display: inline-block;

        &_login {
            font-size: 20px;
        }
    }

    .title-container {
        position: relative;

        .title {
            font-size: 26px;
            // font-weight: 400;
            color: $light_gray;
            margin: 0px auto 40px auto;
            text-align: center;
            font-weight: bold;
        }

        .set-language {
            // color: #fff;
            position: absolute;
            top: 5px;
            right: 0px;
        }
    }

    .show-pwd {
        position: absolute;
        right: 10px;
        top: 7px;
        font-size: 16px;
        color: $dark_gray;
        cursor: pointer;
        user-select: none;
    }

    .thirdparty-button {
        position: absolute;
        right: 35px;
        bottom: 28px;
    }
}

.logoInfo {
    padding-bottom: 15px;
}

.logoInfo span {
    font-size: 22px;
    padding: 0 10px;
    float: left;
}

.el-form--label-left {
    background: rgba(54, 53, 53, 0.5);
}

.logoInfo .logo {
    background: url(../assets/loginLogo.png) no-repeat;
    display: inline-block;
    width: 35px;
    height: 26px;
    float: left;
}

.imgPhoto {
    position: absolute;
    right: 0;
    top: 0;
    padding: 20px;

    .el-image {
        background: rgba(54, 53, 53, 0.5);
        border-radius: 10px;
        margin: 16px;
    }

    img {
        background: rgba(54, 53, 53, 0.5);
        border-radius: 10px;
        margin: 16px;
    }

    .el-form-item__content {
        background: rgba(54, 53, 53, 0.315);
    }
}

.isBorder {
    border: none;
}

.isBorderr {
    border: none;
}

.transition-box {
    position: absolute;
    left: 30%;
    top: 50%;
    width: 520px;
    color: #fff;
    font-size: 30px;
    font-weight: 900;
    text-align: center;
    padding: 15px 35px 15px 35px;
    margin: -200px 0 0 0;
    background: rgba(54, 53, 53, 0.315);
}

.oneWord {
    margin: 0;
    padding: 0;
}
</style>
